package org.news.web.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.news.web.domain.vo.LikesVo;
import org.news.web.domain.bo.LikesBo;
import org.news.web.service.ILikesService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 点赞功能
 *
 * @author Gu Ci
 * @date 2024-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/likes")
public class LikesController extends BaseController {

    private final ILikesService likesService;

    /**
     * 查询点赞功能列表
     */
    @SaCheckPermission("web:likes:list")
    @GetMapping("/list")
    public TableDataInfo<LikesVo> list(LikesBo bo, PageQuery pageQuery) {
        return likesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出点赞功能列表
     */
    @SaCheckPermission("web:likes:export")
    @Log(title = "点赞功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LikesBo bo, HttpServletResponse response) {
        List<LikesVo> list = likesService.queryList(bo);
        ExcelUtil.exportExcel(list, "点赞功能", LikesVo.class, response);
    }

    /**
     * 获取点赞功能详细信息
     *
     * @param likeId 主键
     */
    @SaCheckPermission("web:likes:query")
    @GetMapping("/{likeId}")
    public R<LikesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long likeId) {
        return R.ok(likesService.queryById(likeId));
    }

    /**
     * 新增点赞功能
     */
    @SaCheckPermission("web:likes:add")
    @Log(title = "点赞功能", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LikesBo bo) {
        return toAjax(likesService.insertByBo(bo));
    }

    /**
     * 修改点赞功能
     */
    @SaCheckPermission("web:likes:edit")
    @Log(title = "点赞功能", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LikesBo bo) {
        return toAjax(likesService.updateByBo(bo));
    }

    /**
     * 删除点赞功能
     *
     * @param likeIds 主键串
     */
    @SaCheckPermission("web:likes:remove")
    @Log(title = "点赞功能", businessType = BusinessType.DELETE)
    @DeleteMapping("/{likeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] likeIds) {
        return toAjax(likesService.deleteWithValidByIds(List.of(likeIds), true));
    }

    /**
     * 点赞
     */
    @SaIgnore
    @GetMapping("/like/{userId}/{newsId}")
    public R<Void> like(@PathVariable("userId") Long userId,@PathVariable("newsId") Long newsId){
        return toAjax(likesService.like(userId,newsId));
    }
    /**
     * 取消点赞
     */
    @SaIgnore
    @GetMapping("/nolike/{userId}/{newsId}")
    public R<Void> nolike(@PathVariable Long userId,@PathVariable("newsId") Long newsId){
        return toAjax(likesService.nolike(userId,newsId));
    }
}
